

*-------------------------------------------------------------------------------
*		0. General cleaning and cleaning for long panels
*-------------------------------------------------------------------------------
qui do "${code}/Data Preparation/dataprep"


gen postCat = (date > mdy(3,23,2020)) + (date > mdy(7,23,2020))
label define postCat 1 "0-4 months" 2 "4+ months" 
label values postCat postCat
*replace postCat = 6 if postCat == 5 & ctry == 6


*Generate postCat dummies and mcat for long panel data
gen moy = month(date)
recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
	(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
gen year = date/365
tab postCat, gen(postCat)
drop postCat1

drop if ctrytext == "kenyaipush" & date > mdy(10,8,2020)
drop if ctrytext == "kenyage" & month == 52

*-------------------------------------------------------------------------------
*		1.	Add controls for seasonality samples
*-------------------------------------------------------------------------------

*				1.1 Kenya GE seasonality control
*-------------------------------------------------------------------------------
preserve
qui do "${code}/Data Preparation/dataprep_kenya_ge"
*keep if ctrytext == "kenyage"
merge m:1 date using "${raw}/kenya_ge/kenya_ge_seas", nogen keep(1 3)
gen ctrytext = "kenyage"
tempfile kenyage
save `kenyage' , replace
restore
replace _ctrl_seas = . if ctrytext == "kenyage"
merge m:1 pid date ctrytext using `kenyage', nogen update keep(1 3 4 5)
codebook _ctrl_seas if ctrytext == "kenyage"

*				1.2 Aggregating seasonality controls
*-------------------------------------------------------------------------------
egen _mctrl_seas = mean(_ctrl_seas), by(postCat ctry)
gen __ctrl_seas = _ctrl_seas if date < mdy(3,1,2020)
replace __ctrl_seas = _mctrl_seas if date > mdy(3,1,2020)
replace __ctrl_seas = -1*__ctrl_seas
label var __ctrl_seas "Seasonal Food Security"


*				1.3 Linear time control __year for kenyaipush
*-------------------------------------------------------------------------------
gen __year = date/365
egen myear = mean(__year), by(postCat ctry)
replace __year = myear if postCat > 0



gl ctrys colombia kenyaklps rwanda kenyage nepal kenyaipush  bangladesh drc nigeria sierraleone     

tab ctry highses, mi
tab ctry female, mi
tab ctry highage, mi

drop if postCat == 2 & ctrytext == "kenyaipush"
label var highses "High SES"
label var highage "High Age"
loc highage Age
loc female Gender
loc highses Socioeconomic Status
eststo clear
foreach het in highses female highage {

foreach c in $ctrys {
	*if "`c'" == "kenyage" continue
	*if "`c'" == "nigeria" continue
	*if "`c'" == "bangladesh" continue
	*if "`c'" == "kenyaklps" continue
	*if "`c'" == "drc" continue
		if regexm("`c'", "(rwanda|colombia|bangladesh)") &  "`het'" == "female" continue
	loc ctrl c.year i.mcat
	if regexm("`c'", "(nigeria|bangladesh|sierraleone|drc)") loc ctrl 
	if regexm("`c'", "(nepal|kenyage)") loc ctrl c.__ctrl_seas
	if "`c'" == "kenyaipush" loc ctrl c.__year

	noi di "---------------------------------------"
	noi di "		`s': `c'	: `m'	"
	noi di "---------------------------------------"
	
	replace `het' = . if !inlist(`het', 0,1)
	if "`ctrl'" != "" eststo `c'_`het'_int: noi areg ___depression_nw  (`ctrl' )#`het' i.postCat (1.postCat 2.postCat)#c.`het' if ctrytext == "`c'" , absorb(pid) vce(cluster hhid) noomitted
	*if "`ctrl'" != "" eststo `c'_`het'_int: noi areg ___depression_nw  (`ctrl')#`het' (1.postCat 2.postCat)#`het' if ctrytext == "`c'" , absorb(pid) vce(cluster hhid) 
		if "`ctrl'" == "" eststo `c'_`het'_int: noi areg ___depression_nw  1.postCat 2.postCat (1.postCat 2.postCat)#c.`het' if ctrytext == "`c'" , absorb(pid) vce(cluster hhid)  noomitted
	test 1bn.postCat#c.`het'  2.postCat#c.`het'
	estadd scalar p = `r(p)': `c'_`het'_int
	
}

gl mtitles `" "COL" "KEN1" "RWA" "KEN2" "NPL" "KEN3" "BGD" "DRC" "NGA" "SLE" "'
if "`het'" == "female" gl mtitles `" "KEN1" "KEN2" "NPL" "KEN3" "DRC" "NGA" "SLE" "'
esttab *`het'_int using "${output}/forPaper/TableS9_S10_S11/`het'.tex" , drop(0.postCat) noomitted ///
	keep(*postCat*) label replace title(Heterogeneity in Estimates by ``het'') ///
	mtitles($mtitles ) stats(N p, labels("Obs" "P: row 3 = 0 and row 4 = 0") ) //
	
}

